<?php

namespace App\Console\Commands\Cruise;

use App\Models\Cruise\CruiseCheckErrorModel;
use App\Models\Cruise\CruiseCheckSelfDetailModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Check\CruiseCheckTaskService;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class SelfCheckDetailInvalidCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:selfCheckDetailInvalidCommand';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '工厂自检 - 任务定期作废';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        CruiseCheckSelfDetailModel::query()
                                  ->with(["cruiseOrderProcessRel"])
                                  ->where("status", CruiseCheckSelfDetailModel::STATUS_WAIT)
                                  ->where('templateType', CruiseTemplateModel::TYPE_5)
                                  ->chunkById(
                                      100,
                                      function (Collection $cruiseCheckSelfDetailModelCollection) {
                                          $cruiseCheckSelfDetailModelCollection->each(function (
                                              CruiseCheckSelfDetailModel $cruiseCheckSelfDetailModel
                                          ) {
                                              if (
                                                  $cruiseCheckSelfDetailModel->date < formatDateWithRule(time(), "Ymd")
                                                  &&
                                                  $cruiseCheckSelfDetailModel->cruiseOrderProcessRel->count() <= 1
                                              ) {
                                                  //作废任务表单
                                                  CruiseOrderModel::query()
                                                      ->where('id', $cruiseCheckSelfDetailModel->cruiseOrderId)
                                                      ->whereIn('status', [CruiseOrderModel::STATUS_INIT, CruiseOrderModel::STATUS_NOT_SUBMIT, CruiseOrderModel::STATUS_NOT_REVIEW, CruiseOrderModel::STATUS_NOT_MODIFY, CruiseOrderModel::STATUS_WAIT_MODIFY_EXT, CruiseOrderModel::STATUS_COLLECTED])
                                                      ->get()
                                                      ->each(function (CruiseOrderModel $cruiseOrderModel) {
                                                          //作废订单
                                                          CruiseCheckTaskService::orderCancel($cruiseOrderModel->id, 0, false, CruiseCheckErrorModel::INVALID_TYPE_MISS, '', []);
                                                          $cruiseOrderModel->isDeleted = 1;
                                                          $cruiseOrderModel->save();
                                                      });
                                                  $this->info(time());
                                              }
                                          });
                                      });
        return parent::SUCCESS;
    }
}
